var LOANS = {
		textSuccess: '',
		textError: '',
		val_bk: 0,
		init: function(){
			$("ul.tabs").tabs("div.panes > div");
			$('ul.tabs > li').click(function(){
				$('div.tab-active').text($('a',$(this)).html());
			});
			LOANS.change();
			LOANS.period_add();
			LOANS.period_change();
		},
		change:function(){
			$('input.period-text').click(function(){
				if($(this).attr('readonly')=='readonly'){
					$(this).removeAttr('readonly');
					$(this).removeClass('none')
					$(this).addClass('active');
					LOANS.val_bk = $(this).val();
					$('div.save-result').text('');
				}
			});
			$('input.period-text').blur(function(){
				if($(this).attr('readonly')!='readonly'){
					$(this).attr('readonly', 'readonly');
					$(this).removeClass('active')
					$(this).addClass('none');
					if($(this).val()==''){
						//$(this).val('0');
					}
					if($(this).val()!=LOANS.val_bk){
						LOANS.update($(this));
					}
				}
			});
			
		},
		update: function(obj){
			$('div.save-result').text('');
			$.ajax({
				type: "POST",
				url: '/admin/bankings/bankingToLoanColumnsUpdate',
				data: {
					val: obj.val(),
					banking_loan_id: obj.attr('data-cur'),
					bank_id: obj.attr('data-bank'),
					banking_loan_column_id: obj.attr('data-period')
				},
				cache: false,
				success: function(data){
					if(parseInt(data, 10) > 0){
						$('div.save-result').text(LOANS.textSuccess);
					}else{
						$('div.save-result').text(LOANS.textError);
					}
				}
			});
		},
		period_change:function(){
			$('input.period-change').click(function(){
				if($(this).attr('readonly')=='readonly'){
					$(this).removeAttr('readonly');
					$(this).removeClass('none')
					$(this).addClass('active');
					LOANS.val_bk = $(this).val();
					$('div.save-result').text('');
				}
			});
			$('input.period-change').blur(function(){
				if($(this).attr('readonly')!='readonly'){
					$(this).attr('readonly', 'readonly');
					$(this).removeClass('active')
					$(this).addClass('none');
					if($(this).val()==''){
						$(this).val(LOANS.val_bk);
					}
					if($(this).val()!=LOANS.val_bk){
						LOANS.period_update($(this));
					}
					
				}
			});
		},
		period_update: function(obj){
			$('div.save-result').text('');
			$.ajax({
				type: "POST",
				url: '/admin/bankings/bankingToLoanColumnsInsert',
				data: {
					name: obj.val(),
					id: obj.attr('data-id')
				},
				cache: false,
				success: function(data){
					if(parseInt(data, 10) > 0){
						$('div.save-result').text(LOANS.textSuccess);
					}else{
						$('div.save-result').text(LOANS.textError);
					}
				}
			});
		},
		period_add:function(){
			$('input.period-add').click(function(){
				if($('input.period-add').attr('readonly')=='readonly'){
					$('input.period-add').val('');
					$('input.period-add').removeAttr('readonly');
					$('input.period-add').removeClass('none')
					$('input.period-add').addClass('active');
					$('div.save-result').text('');
				}
			});
			$('input.period-add').blur(function(){
				if($(this).attr('readonly')!='readonly'){
					$('input.period-add').attr('readonly', 'readonly');
					$('input.period-add').removeClass('active')
					$('input.period-add').addClass('none');
					if($(this).val()==''){
						$('input.period-add').val('+');
					}else{
						LOANS.period_insert($(this));
					}
					
				}
			});
		},
		period_insert: function(obj){
			$('div.save-result').text('');
			$.ajax({
				type: "POST",
				url: '/admin/bankings/bankingToLoanColumnsInsert',
				data: {
					name: obj.val()
				},
				cache: false,
				success: function(data){
					if(parseInt(data, 10) > 0){
						$('input.period-add').each(function(){
							var head_td = $('<td class="head"></td>');
							var head_indput = $('<input type="text" data-cur="'+$(this).attr('data-cur')+'" class="period-add none" readonly="true" name="period_add" value="+"/>');
							head_td.append(head_indput);
							$(this).parent().parent().append(head_td);
							var head_cur = $('<input type="text" class="period-change none" data-id="'+data+'" readonly="true" name="period_add" value="'+$(obj).val()+'"/>');
							$(this).parent().append(head_cur);
							$(this).remove();
							$('tr.tr-name', $('table.tbl-'+$(this).attr('data-cur'))).each(function(){
								var input = $('<input type="text" data-cur="'+$(this).attr('data-cur')+'" data-bank="'+$(this).attr('data-bank')+'" data-period="'+data+'" class="period-text none" readonly="true" name="period" value=""/>');
								var td_last = $('td.last', $(this));
								td_last.append(input);
								td_last.removeClass('last');
								var td_new = $('<td class="name last"></td>');
								td_new.html('&nbsp;');
								$(this).append(td_new);
								
							});
						});
						
						LOANS.change();
						LOANS.period_add();
						LOANS.period_change();
						$('div.save-result').text(LOANS.textSuccess);
					}else{
						$('div.save-result').text(LOANS.textError);
					}
				}
			});
		},
}